package org.black.phoneUtility.sms.reply.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromMatchSmsWithKeyword;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SmsReplyAddEditMatchSmsKeywordDialogFragment extends
		DialogFragment {
	public static final String TAG = SmsReplyAddEditMatchSmsKeywordDialogFragment.class
			.getName();
	public static final String ARGUMENTS_SMS_REPLY_ADD_EDIT_MATCH_SMS_KEYWORDS_OBJECT = "arguments_sms_reply_add_edit_match_sms_keywords_object";
	public static final String ARGUMENTS_SMS_REPLY_ADD_EDIT_MATCH_SMS_KEYWORDS_COME_FROM = "arguments_sms_reply_add_edit_match_sms_keywords_come_from";

	public static final int MATCH_SMS_KEYWORDS_COME_FROM_RESPONSE_NUMBER_FROM = 129;
	public static final int MATCH_SMS_KEYWORDS_COME_FROM_NONE_RESPONSE_NUMBER = 131;

	public interface SmsReplyAddEditMatchSmsKeywordDialogFragmentInterface {
		public void onSmsReplyAddEditMatchSmsKeywordDialogFragmentPositiveClick(
				int matchSmsKeywordFor, String keyword, boolean onAdding);

		public void onSmsReplyAddEditMatchSmsKeywordDialogFragmentNegativeClick();
	}

	private SmsReplyAddEditMatchSmsKeywordDialogFragmentInterface smsReplyAddEditMatchSmsKeywordDialogFragmentInterface;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyAddEditMatchSmsKeywordDialogFragmentInterface = (SmsReplyAddEditMatchSmsKeywordDialogFragmentInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsReplyAddEditMatchSmsKeywordDialogFragmentInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final Bundle bundle = getArguments() == null ? savedInstanceState
				: getArguments();
		final Object matchKeywordObjet = bundle
				.getSerializable(ARGUMENTS_SMS_REPLY_ADD_EDIT_MATCH_SMS_KEYWORDS_OBJECT);
		View view = getActivity()
				.getLayoutInflater()
				.inflate(
						R.layout.sms_reply_add_edit_sms_messages_contain_a_keyword_dialog,
						null);

		EditText keywordEditText = (EditText) view
				.findViewById(R.id.smsReplyAddEditMatchSmsKeywordEditText);
		if (matchKeywordObjet != null
				&& matchKeywordObjet instanceof ResponseNumberFromMatchSmsWithKeyword) {
			keywordEditText
					.setText(((ResponseNumberFromMatchSmsWithKeyword) matchKeywordObjet)
							.getKeyword());
		} else if (matchKeywordObjet != null
				&& matchKeywordObjet instanceof NoneReplyNumberFromMatchSmsWithKeyword) {
			keywordEditText
					.setText(((NoneReplyNumberFromMatchSmsWithKeyword) matchKeywordObjet)
							.getKeyword());
		}

		builder.setTitle(
				R.string.sms_add_edit_sms_messages_contain_keywords_title)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								EditText keywordEditText = (EditText) getDialog()
										.findViewById(
												R.id.smsReplyAddEditMatchSmsKeywordEditText);
								if (matchKeywordObjet != null
										&& matchKeywordObjet instanceof ResponseNumberFromMatchSmsWithKeyword) {
									((ResponseNumberFromMatchSmsWithKeyword) matchKeywordObjet)
											.setKeyword(keywordEditText
													.getText().toString());
								} else if (matchKeywordObjet != null
										&& matchKeywordObjet instanceof NoneReplyNumberFromMatchSmsWithKeyword) {
									((NoneReplyNumberFromMatchSmsWithKeyword) matchKeywordObjet)
											.setKeyword(keywordEditText
													.getText().toString());
								}
								smsReplyAddEditMatchSmsKeywordDialogFragmentInterface
										.onSmsReplyAddEditMatchSmsKeywordDialogFragmentPositiveClick(
												bundle.getInt(
														ARGUMENTS_SMS_REPLY_ADD_EDIT_MATCH_SMS_KEYWORDS_COME_FROM,
														-1),
												keywordEditText.getText()
														.toString(),
												matchKeywordObjet == null ? true
														: false);
							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});

		builder.setView(view);
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsReplyAddEditMatchSmsKeywordDialogFragmentInterface = null;
	}

}
